package com.tengits.react.amap3d.maps;

import android.view.View;

import com.tengits.react.amap3d.AMapUtils;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.annotations.ReactProp;

import java.util.HashMap;
import java.util.Map;

public class AMapMarkerManager extends ViewGroupManager<AMapMarker> {
  public final static int UPDATE = 1;
  public final static int ACTIVE = 2;
  public final static int LOCK_TO_SCREEN = 3;

  @Override
  public String getName() {
      return "AMapMarker";
  }

  @Override
  public AMapMarker createViewInstance(ThemedReactContext reactContext) {
      return new AMapMarker(reactContext);
  }

  @Override
  public void addView(AMapMarker marker, View view, int index) {
      if (view != null) {
        super.addView(marker, view, index);
      }
  }

  @Override
  public Map getExportedCustomDirectEventTypeConstants(){
      return MapBuilder.of(
              "onPress", MapBuilder.of("registrationName", "onPress"),
              "onDragStart", MapBuilder.of("registrationName", "onDragStart"),
              "onDrag", MapBuilder.of("registrationName", "onDrag"),
              "onDragEnd", MapBuilder.of("registrationName", "onDragEnd"),
              "onInfoWindowPress", MapBuilder.of("registrationName", "onInfoWindowPress")
      );
  }

  @Override
  public Map<String, Integer> getCommandsMap() {
      Map result = new HashMap(){{
        put("update", UPDATE);
        put("active", ACTIVE);
        put("lockToScreen", LOCK_TO_SCREEN);
      }};
      return result;
  }

  @Override
  public void receiveCommand(AMapMarker marker, int commandId, ReadableArray args) {
      switch (commandId) {
        case UPDATE:
          marker.updateIcon();
          break;
        case ACTIVE:
          marker.setActive(true);
          break;
        case LOCK_TO_SCREEN:
          marker.lockToScreen(args);
          break;
      }
  }

    @ReactProp(name = "title")
    public void setTitle(AMapMarker marker, String title) {
        marker.setTitle(title);
    }

    @ReactProp(name = "description")
    public void setSnippet(AMapMarker marker, String description) {
        marker.setSnippet(description);
    }

    @ReactProp(name = "coordinate")
    public void setCoordinate(AMapMarker view, ReadableMap coordinate) {
        view.setPosition(AMapUtils.toLatLng(coordinate));
    }

    @ReactProp(name = "flat")
    public void setFlat(AMapMarker marker, Boolean flat) {
        marker.setFlat(flat);
    }

    @ReactProp(name = "opacity")
    public void setOpacity(AMapMarker marker, float opacity) {
        marker.setOpacity(opacity);
    }

    @ReactProp(name = "draggable")
    public void setDraggable(AMapMarker marker, Boolean draggable) {
        marker.setDraggable(draggable);
    }

    @ReactProp(name = "clickDisabled")
    public void setClickDisabled(AMapMarker marker, Boolean disabled) {
        marker.setClickDisabled(disabled);
    }

    @ReactProp(name = "infoWindowDisabled")
    public void setInfoWindowDisabled(AMapMarker marker, Boolean disabled) {
        marker.setInfoWindowDisabled(disabled);
    }

    @ReactProp(name = "active")
    public void setSelected(AMapMarker marker, Boolean active) {
        marker.setActive(active);
    }

    @ReactProp(name = "color")
    public void setIcon(AMapMarker marker, String icon) {
        marker.setIconColor(icon);
    }

    @ReactProp(name = "image")
    public void setImage(AMapMarker marker,String image) {
        marker.setImage(image);
    }

    @ReactProp(name = "zIndex")
    public void setZIndez(AMapMarker marker, float zIndex) {
        marker.setZIndex(zIndex);
    }

    @ReactProp(name = "anchor")
    public void setAnchor(AMapMarker view, ReadableMap coordinate) {
        view.setAnchor(coordinate.getDouble("x"), coordinate.getDouble("y"));
    }
}
